<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>SmartPing Dashboard</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<link href="assets/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="assets/css/font-awesome.css" rel="stylesheet">
<link href="assets/css/style.css" rel="stylesheet">
<link href="assets/css/pages/dashboard.css" rel="stylesheet">
  <link href="assets/css/pages/plans.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
<body>
<div class="navbar navbar-fixed-top">
  <div class="navbar-inner">
    <div class="container">
      <span id="cloudbrand" class="brand" style="margin-right: -15px;"></span><a class="brand" href="index.html">SmartPing Dashbord<span id="agentname"></span></a>
      <span id="banner_last_ck_time" class="pull-right " style="margin-top: 10px;"></span>
    </div>
    <!-- /container -->
  </div>
  <!-- /navbar-inner -->
</div>
<!-- /navbar -->
<div class="subnavbar">
  <div class="subnavbar-inner">
    <div class="container">
      <ul class="mainnav">
        <li><a href="index.html"><i class="icon-mail-forward"></i><span>正向Ping</span> </a> </li>
        <li><a href="reverse.html"><i class="icon-mail-reply"></i><span>反向Ping</span> </a> </li>
        <li  class="active"><a href="topology.html"><i class="icon-bar-chart"></i><span>Ping拓扑</span> </a> </li>
        <li><a href="mapping.html"><i class="icon-map-marker"></i><span>全国延迟</span> </a> </li>
        <li ><a href="tools.html"><i class="icon-wrench"></i><span>检测工具</span> </a> </li>
        <li><a  id="cfgUrl"  href="config.html"><i class="icon-cog"></i><span>系统配置</span> </a> </li>
      </ul>
    </div>
    <!-- /container --> 
  </div>
  <!-- /subnavbar-inner --> 
</div>
<!-- /subnavbar -->
<div class="main" style="margin-top:-20px;">
  <div class="main-inner">
    <div class="container">
      <div class="row">
        <div class="span10">
          <div id="echarts" style="width:100%;"></div>
        </div>

        <div class="span2">
          <div class="pricing-plans plans-1" style="margin-left: 5px;">
            <div class="plan-container">
              <div class="plan green">
                <div class="plan-header" >
                  <div class="plan-title" onclick="javascript:window.location.href='alerts.html'">
                    <i class="icon-warning-sign"></i> 查看报警记录
                  </div> <!-- /plan-title -->
                </div> <!-- /plan-header -->
              </div> <!-- /plan -->
            </div> <!-- /plan-container -->
          </div>
          <!--
          <div style="text-align: center;margin-bottom: 15px;">
            <button class="alert alert-danger" style="width:100%;height:50px;margin-bottom: -5px;text-align: center" onclick="javascript:window.location.href='alerts.html'"><i class="icon-warning-sign"></i> 查看报警记录</button>
          </div>
          -->
          <div class="widget widget-table action-table" style="margin-top: 60px;">
            <div class="widget-header"> <i class="icon-th-list"></i>
              <h3>拓扑列表</h3>
            </div>
            <div class="widget-content">
              <table class="table table-striped table-bordered">
                <tbody class="agentlist"></tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- /container --> 
  </div>
  <!-- /main-inner --> 
</div>
<!-- /main -->

<div class="footer">
  <div class="footer-inner">
    <div class="container">
      <div class="row">
        <div class="span12"> &copy; 2017 - 2020 <a target="_blank" href="http://smartping.org" >SmartPing.org</a> <span style="float:right" id="verion"></span></div>
        <!-- /span12 --> 
      </div>
      <!-- /row --> 
    </div>
    <!-- /container --> 
  </div>
  <!-- /footer-inner --> 
</div>
<!-- /footer --> 
<!-- Le javascript
================================================== --> 
<!-- Placed at the end of the document so the pages load faster --> 
<script src="assets/js/jquery-1.7.2.min.js"></script>
<script src="assets/js/bootstrap.js"></script>
<script src="assets/js/echarts.min.js"></script>
<script src="assets/js/funcs.js"></script>
<script>
    $(function(){
        var ajaxCnt = 0
        $("#echarts").height($(window).height()-220);
        $.getJSON("/api/config.json",function(result){
            $("#agentname").html(" ("+result["Name"]+")")
            AgentMode(result["Mode"])
            $("#verion").html("Version: "+result["Ver"])
            setTimeout('Refresh()',result["Base"]["Refresh"]*60*1000);
            var dataarea = [];
            var dataline = [];
            option = {
                tooltip: {
                    show: true
                },
                animationDurationUpdate: 1500,
                animationEasingUpdate: 'quinticInOut',
                series : [
                    {
                        type: 'graph',
                        layout: 'circular',
                        symbolSize: result["Topology"]["Tsymbolsize"],
                        focusNodeAdjacency:true,
                        roam: true,
                        label: {
                            normal: {
                                show: true
                            }
                        },
                        edgeSymbol: ['circle', 'arrow'],
                        edgeSymbolSize: [3, 15],
                        edgeLabel: {
                            normal: {
                                textStyle: {
                                    fontSize: 15
                                }
                            }
                        },
                        data : dataarea,
                        links: dataline,
                        lineStyle: {
                            normal: {
                                opacity: 0.9,
                                width: result["Topology"]["Tline"],
                                curveness: 0
                            }
                        }
                    }
                ]
            };
            var myChart = echarts.init(document.getElementById('echarts'));
            myChart.setOption(option);
            FromObj = new Object();
            FromToObj = new Object();
            $.each(result["Network"], function(i, network){
                ArrObj = new Object()
                ArrObj["Name"]=network["Name"]
                ArrObj["Addr"]=network["Addr"]
                ArrObj["Type"]="To"
                ArrObj["Color"]= "green"
                if(network["Topology"].length>0){
                    ArrObj["Color"]= "gray"
                    ArrObj["Type"]="From"
                }
                FromObj[network["Addr"]]=ArrObj
                $.each(network["Topology"], function(i, topology){
                    ArrObj = new Object()
                    ArrObj["From"] = network["Name"]
                    ArrObj["To"] = result["Network"][topology["Addr"]]["Name"]
                    ArrObj["Curveness"] = 0.2
                    ArrObj["Color"] ="gray"
                    FromToObj[network["Addr"]+"-"+topology["Addr"]]= ArrObj
                })
            });
            $.each(FromObj, function(i, network){
                if(network["Type"]=="To"){
                    return true
                }
                ajaxCnt = ajaxCnt + 1
                $(".agentlist").append("<tr><td><i class='icon-spinner icon-spin animated alerticon-" + network["Name"] + "'  data-toggle='tooltip' title='' data-placement='bottom'></i>&nbsp;" + network["Name"] + "</td></tr>");
            });

            $.each(FromObj,function(i,FromObjInfo){
                if(FromObjInfo["Type"]=="To"){
                    return true
                }
                $.ajax({
                    dataType: "json",
                    url: '/api/proxy.json?g=http://' + FromObjInfo["Addr"] + ':' + result['Port'] + '/api/topology.json',
                    success: function (topodata) {
                        ajaxCnt = ajaxCnt - 1
                        $(".alerticon-" + FromObjInfo["Name"] + "").remove();
                        $.each(topodata,function(ToAddr,Flag){
                            if(FromToObj.hasOwnProperty(FromObjInfo["Addr"]+"-"+ToAddr)){
                                if(Flag == "true"){
                                    FromToObj[FromObjInfo["Addr"]+"-"+ToAddr]["Curveness"]=0
                                    FromToObj[FromObjInfo["Addr"]+"-"+ToAddr]["Color"]="green"
                                }else{
                                    FromToObj[FromObjInfo["Addr"]+"-"+ToAddr]["Curveness"]=0.2
                                    FromToObj[FromObjInfo["Addr"]+"-"+ToAddr]["Color"]="red"
                                    if ($("#alert").length < 1) {
                                        $(".main").append('<audio id="alert" style="display:none"  autoplay="autoplay"  controls="controls" loop="loop"><source src="' + result["Topology"]["Tsound"] + '" type="audio/mp3"  /></audio>');
                                    }
                                    //console.log(FromObjInfo["Addr"]+"-"+ToAddr)
                                }
                            }
                        })
                        FromObj[FromObjInfo["Addr"]]["Color"]="green"
                        //console.log(ajaxCnt)
                        if(ajaxCnt==0){
                            Dawr()
                        }
                    },
                    timeout: result["Base"]["Timeout"] * 1000
                }).fail(function (xhr, status) {
                    ajaxCnt = ajaxCnt - 1
                    $(".alerticon-" + FromObjInfo["Name"] + "").removeClass("icon-spinner").removeClass("icon-spin").removeClass("animated");
                    $(".alerticon-" + FromObjInfo["Name"] + "").addClass("icon-warning-sign");
                    $(".alerticon-" + FromObjInfo["Name"] + "").attr("title",xhr.responseText);
                    $(".alerticon-" + FromObjInfo["Name"] + "").tooltip();
                    FromObj[FromObjInfo["Addr"]]["Color"]="red"
                    if ($("#alert").length < 1) {
                       $(".main").append('<audio id="alert" style="display:none"  autoplay="autoplay"  controls="controls" loop="loop"><source src="' + result["Topology"]["Tsound"]+ '" type="audio/mp3"  /></audio>');
                    }
                    if(ajaxCnt==0){
                        Dawr()
                    }
                });
            })
            function Dawr(){
                //console.log(FromObj)
                $.each(FromObj,function(i,Area){
                    dataarea.push({
                        name: Area["Name"],
                        draggable: "true",
                        itemStyle: {
                            normal: {
                                color: Area["Color"]
                            }
                        }
                    });
                })
                $.each(FromToObj,function(i,Line){
                    //console.log("=== ",i,Line)
                    dataline.push({
                        source: Line["From"],
                        target: Line["To"],
                        lineStyle: {
                            normal: {curveness: Line["Curveness"], color: Line["Color"]}
                        }
                    })
                })
                myChart.setOption(option);
            }

        }).fail(function (xhr, status) {
            alert(xhr.responseText);
            Refresh();
        });

    });
</script>
</body>
</html>
